package com.caishi.lkx.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caishi.lkx.user.mapper.UserActiveMapper;
import com.caishi.lkx.user.model.UserActive;
import com.caishi.lkx.user.service.IUserActiveService;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.File;


@Service
public class UserActiveServiceImpl extends ServiceImpl<UserActiveMapper, UserActive> implements IUserActiveService {
    @Value("${spring.profiles.active}")
    private String activeProfile;
    @Override
    public String getLocationWithIp(String ip) throws Exception {
//        ResourceLoader resourceLoader = new DefaultResourceLoader();
//        Resource resource = resourceLoader.getResource("classpath:ip2region_v4.xdb");
//
//        Searcher searcher = Searcher.newWithFileOnly(resource.getFile().getPath());
        Searcher searcher=null;
        if("dev".equals(activeProfile)){
             searcher = Searcher.newWithFileOnly("F:\\server1\\question-bank-services-no-git\\question-server\\src\\main\\resources\\ip2region_v4.xdb");
        }else if ("pro".equals(activeProfile)){
            searcher = Searcher.newWithFileOnly("/root/ip2region_v4.xdb");
        }

//

        if (searcher != null) {
            return  searcher.search(ip);
        }else{
            return "未知";
        }
    }
}
